using System;
using System.Linq;
using System.Runtime.Serialization;
using System.Data.Objects.DataClasses;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Collections.Generic;
using System.Data.Objects;

namespace Acme.AuditExample.EFDAL.Entity
{
	/// <summary>
	 /// The collection to hold 'Customer' entities
	/// </summary>
	[EdmEntityTypeAttribute(NamespaceName="Acme.AuditExample.EFDAL.Entity", Name="Customer")]
	[Serializable()]
	[DataContractAttribute(IsReference = true)]
	public partial class Customer : NHEntityObject, IBusinessObject
	{
		#region FieldNameConstants Enumeration

		/// <summary>
		/// Enumeration to define each property that maps to a database field for the 'Customer' table.
		/// </summary>
		public enum FieldNameConstants
		{
			 /// <summary>
			 /// Field mapping for the 'CustomerId' property
			 /// </summary>
			[System.ComponentModel.Description("Field mapping for the 'CustomerId' property")]
			CustomerId,
			 /// <summary>
			 /// Field mapping for the 'Name' property
			 /// </summary>
			[System.ComponentModel.Description("Field mapping for the 'Name' property")]
			Name,
			 /// <summary>
			 /// Field mapping for the 'CreatedBy' property
			 /// </summary>
			[System.ComponentModel.Description("Field mapping for the 'CreatedBy' property")]
			CreatedBy,
			 /// <summary>
			 /// Field mapping for the 'CreatedDate' property
			 /// </summary>
			[System.ComponentModel.Description("Field mapping for the 'CreatedDate' property")]
			CreatedDate,
			 /// <summary>
			 /// Field mapping for the 'ModifiedBy' property
			 /// </summary>
			[System.ComponentModel.Description("Field mapping for the 'ModifiedBy' property")]
			ModifiedBy,
			 /// <summary>
			 /// Field mapping for the 'ModifiedDate' property
			 /// </summary>
			[System.ComponentModel.Description("Field mapping for the 'ModifiedDate' property")]
			ModifiedDate,
		}
		#endregion

		#region Constructors

		/// <summary>
		/// Initializes a new instance of the Acme.AuditExample.EFDAL.Entity.Customer class
		/// </summary>
		public Customer()
		{
			this.CustomerId = Guid.NewGuid();
		}

		/// <summary>
		/// Initializes a new instance of the Acme.AuditExample.EFDAL.Entity.Customer class with a defined primary key
		/// </summary>
		public Customer(System.Guid customerId)
			: this()
		{
			this.CustomerId = customerId;
		}

		#endregion

		#region Properties

		/// <summary>
		/// The property that maps back to the database 'CustomerId' field
		/// </summary>
		[EdmScalarPropertyAttribute(EntityKeyProperty = true, IsNullable = false)]
		[DataMemberAttribute()]
		public virtual System.Guid CustomerId
		{
			get { return _customerId; }
			protected set
			{
				ReportPropertyChanging("CustomerId");
				_customerId = value;
				ReportPropertyChanged("CustomerId");
			}
		}

		/// <summary>
		/// The property that maps back to the database 'Name' field
		/// </summary>
		[EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = true)]
		[DataMemberAttribute()]
		public virtual string Name
		{
			get { return _name; }
			set
			{
				if ((value != null) && (value.Length > GetMaxLength(FieldNameConstants.Name))) throw new Exception(string.Format(GlobalValues.ERROR_DATA_TOO_BIG, value, "Customer.Name", GetMaxLength(FieldNameConstants.Name)));
				ChangingEventArgs<string> eventArg = new ChangingEventArgs<string>(value, "Name");
				if (eventArg.Cancel) return;
				ReportPropertyChanging("Name");
				_name = eventArg.Value;
				ReportPropertyChanged("Name");
			}
		}

		/// <summary>
		/// The audit field for the 'Created By' column.
		/// </summary>
		[EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = true)]
		[DataMemberAttribute()]
		public virtual string CreatedBy
		{
			get { return _createdby; }
			internal set
			{
				ChangingEventArgs<string> eventArg = new ChangingEventArgs<string>(value, "CreatedBy");
				if (eventArg.Cancel) return;
				ReportPropertyChanging("CreatedBy");
				_createdby = eventArg.Value;
				ReportPropertyChanged("CreatedBy");
			}
		}

		/// <summary>
		/// The internal reference variable for the 'createdby' property
		/// </summary>
		protected string _createdby;

		/// <summary>
		/// The audit field for the 'Created Date' column.
		/// </summary>
		[EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = true)]
		[DataMemberAttribute()]
		public virtual DateTime? CreatedDate
		{
			get { return _createddate; }
			internal set
			{
				ChangingEventArgs<DateTime?> eventArg = new ChangingEventArgs<DateTime?>(value, "CreatedDate");
				if (eventArg.Cancel) return;
				ReportPropertyChanging("CreatedDate");
				_createddate = eventArg.Value;
				ReportPropertyChanged("CreatedDate");
			}
		}

		/// <summary>
		/// The internal reference variable for the 'createddate' property
		/// </summary>
		protected DateTime? _createddate;

		/// <summary>
		/// The audit field for the 'Modified By' column.
		/// </summary>
		[EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = true)]
		[DataMemberAttribute()]
		public virtual string ModifiedBy
		{
			get { return _modifiedby; }
			internal set
			{
				ChangingEventArgs<string> eventArg = new ChangingEventArgs<string>(value, "ModifiedBy");
				if (eventArg.Cancel) return;
				ReportPropertyChanging("ModifiedBy");
				_modifiedby = eventArg.Value;
				ReportPropertyChanged("ModifiedBy");
			}
		}

		/// <summary>
		/// The internal reference variable for the 'modifiedby' property
		/// </summary>
		protected string _modifiedby;

		/// <summary>
		/// The audit field for the 'Modified Date' column.
		/// </summary>
		[EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = true)]
		[DataMemberAttribute()]
		public virtual DateTime? ModifiedDate
		{
			get { return _modifieddate; }
			internal set
			{
				ChangingEventArgs<DateTime?> eventArg = new ChangingEventArgs<DateTime?>(value, "ModifiedDate");
				if (eventArg.Cancel) return;
				ReportPropertyChanging("ModifiedDate");
				_modifieddate = eventArg.Value;
				ReportPropertyChanged("ModifiedDate");
			}
		}

		/// <summary>
		/// The internal reference variable for the 'modifieddate' property
		/// </summary>
		protected DateTime? _modifieddate;

		[EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = true)]
		[DataMemberAttribute()]
		internal virtual byte[] TimeStamp
		{
			get { return _timestamp; }
			set
			{
				ChangingEventArgs<byte[]> eventArg = new ChangingEventArgs<byte[]>(value, "TimeStamp");
				if (eventArg.Cancel) return;
				ReportPropertyChanging("TimeStamp");
				_timestamp = eventArg.Value;
				ReportPropertyChanged("TimeStamp");
			}
		}

		/// <summary>
		/// The internal reference variable for the 'timestamp' property
		/// </summary>
		protected byte[] _timestamp;

		#endregion

		#region Events

		/// <summary>
		/// The internal reference variable for the 'CustomerId' property
		/// </summary>
		protected System.Guid _customerId;

		/// <summary>
		/// The internal reference variable for the 'Name' property
		/// </summary>
		protected string _name;

		#endregion

		#region GetMaxLength

		/// <summary>
		/// Gets the maximum size of the field value.
		/// </summary>
		public static int GetMaxLength(FieldNameConstants field)
		{
			switch (field)
			{
				case FieldNameConstants.CustomerId:
					return 0;
				case FieldNameConstants.Name:
					return 100;
			}
			return 0;
		}

		int IBusinessObject.GetMaxLength(Enum field)
		{
			return GetMaxLength((FieldNameConstants)field);
		}

		#endregion

		#region IsParented

		/// <summary>
		/// Determines if this object is part of a collection or is detached
		/// </summary>
		[System.ComponentModel.Browsable(false)]
		public virtual bool IsParented
		{
		  get { return (this.EntityState != System.Data.EntityState.Detached); }
		}

		#endregion

		#region IsEquivalent

		/// <summary>
		/// Determines if all of the fields for the specified object exactly matches the current object.
		/// </summary>
		/// <param name="item">The object to compare</param>
		public override bool IsEquivalent(NHEntityObject item)
		{
			if (item == null) return false;
			if (!(item is Customer)) return false;
			Customer o = item as Customer;
			return (
				o.CustomerId == this.CustomerId &&
				o.Name == this.Name
				);
		}

		#endregion

		#region Navigation Properties

		#endregion

		#region GetAuditRecords

		/// <summary>
		/// Return audit records for this entity
		/// </summary>
		/// <returns>A set of audit records for the current record based on primary key</returns>
		public IEnumerable<Acme.AuditExample.EFDAL.Audit.CustomerAudit> GetAuditRecords()
		{
			return Acme.AuditExample.EFDAL.Audit.CustomerAudit.GetValues(this.CustomerId);
		}

		/// <summary>
		/// Return audit records for this entity
		/// </summary>
		/// <param name="pageOffset">The page offset needed for pagination starting from page 1</param>
		/// <param name="recordsPerPage">The number of records to be returned on a page.</param>
		/// <returns>A set of audit records for the current record based on primary key</returns>
		public AuditPaging<Acme.AuditExample.EFDAL.Audit.CustomerAudit> GetAuditRecords(int pageOffset, int recordsPerPage)
		{
			return Acme.AuditExample.EFDAL.Audit.CustomerAudit.GetValues(pageOffset, recordsPerPage, null, null, this.CustomerId);
		}

		/// <summary>
		/// Return audit records for this entity
		/// </summary>
		/// <param name="pageOffset">The page offset needed for pagination starting from page 1</param>
		/// <param name="recordsPerPage">The number of records to be returned on a page.</param>
		/// <param name="startDate">The starting date used when searching for records.</param>
		/// <param name="endDate">The ending date used when searching for records.</param>
		/// <returns>A set of audit records for the current record based on primary key</returns>
		public AuditPaging<Acme.AuditExample.EFDAL.Audit.CustomerAudit> GetAuditRecords(int pageOffset, int recordsPerPage, DateTime? startDate, DateTime? endDate)
		{
			return Acme.AuditExample.EFDAL.Audit.CustomerAudit.GetValues(pageOffset, recordsPerPage, startDate, endDate, this.CustomerId);
		}

		#endregion

	}

	partial class Customer : ICreatedAudit, IModifiedAudit, IConcurrencyAudit
	{
		#region ICreatedAudit Members

		string ICreatedAudit.CreatedBy
		{
			get { return this.CreatedBy; }
		}

		DateTime? ICreatedAudit.CreatedDate
		{
			get { return this.CreatedDate; }
		}

		#endregion

		#region IModifiedAudit Members

		string IModifiedAudit.ModifiedBy
		{
			get { return this.ModifiedBy; }
		}

		DateTime? IModifiedAudit.ModifiedDate
		{
			get { return this.ModifiedDate; }
		}

		#endregion

		#region IConcurrencyAudit Members

		byte[] IConcurrencyAudit.TimeStamp
		{
			get { return this.TimeStamp; }
		}

		#endregion

	}

}
